package com.tangoj.model.vo;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableName;
import com.tangoj.model.dto.questionsubmit.JudgeInfo;
import com.tangoj.model.entity.QuestionSubmit;
import lombok.Data;

import java.io.Serializable;
import java.util.Date;

/**
 * 题目提交表
 *
 * @TableName question_submit
 */
@Data
public class QuestionSubmitVO implements Serializable {
    /**
     * id
     */
    private Long id;

    /**
     * 编程语言
     */
    private String language;

    /**
     * 用户代码
     */
    private String code;

    /**
     * 判题信息（json对象）
     */
    private JudgeInfo judgeInfo;

    /**
     * 提交状态 status（0-执行通过 1-错误解答 2-超出内存限制 3-超出输出限制 4-超出时间限制 5-执行出错 6-内部错误 7-编译错误 8-超时）
     */
    private Integer status;

    /**
     * 判题状态 judgeStatus（0-待判题 1-判题中 2-成功 3-失败）
     */
    private Integer judgeStatus;

    /**
     * 题目Id
     */
    private Long questionId;

    /**
     * 创建用户 id
     */
    private Long userId;

    /**
     * 创建时间
     */
    private Date createTime;


    @TableField(exist = false)
    private static final long serialVersionUID = 1L;


    /**
     * 对象转包装类
     *
     * @param questionSubmit
     * @return
     */
    public static QuestionSubmitVO objToVo(QuestionSubmit questionSubmit) {

        QuestionSubmitVO questionSubmitVO = new QuestionSubmitVO();
        BeanUtil.copyProperties(questionSubmit, questionSubmitVO);

        String judgeInfoStr = questionSubmit.getJudgeInfo();
        JudgeInfo judgeInfo = JSONUtil.toBean(judgeInfoStr, JudgeInfo.class);

        questionSubmitVO.setJudgeInfo(judgeInfo);

        return questionSubmitVO;
    }

    /**
     * 包装类转对象
     *
     * @param questionSubmitVO
     * @return
     */
    public static QuestionSubmit voToObj(QuestionSubmitVO questionSubmitVO) {
        QuestionSubmit questionSubmit = new QuestionSubmit();

        questionSubmit.setJudgeInfo(JSONUtil.toJsonStr(questionSubmitVO.getJudgeInfo()));

        return questionSubmit;
    }
}
